﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Breeze.WebApi2;
using System.Data.Entity;
using Idte.Application.PDN.Models;
using Breeze.ContextProvider.EF6;
using Breeze.ContextProvider;
using Newtonsoft.Json.Linq;
using LinqKit;

namespace Idte.Application.PDN.Controllers
{
    public class QuestionComparer : IEqualityComparer<CategoryQuestion>
    {
        public bool Equals(CategoryQuestion x, CategoryQuestion y)
        {
            return x.Question.Id == y.Question.Id;
        }

        public int GetHashCode(CategoryQuestion obj)
        {
            throw new NotImplementedException();
        }
    }
    [BreezeController]
    public class DataController : ApiController
    {
        readonly EFContextProvider<PDNContext> _contextProvider =
            new EFContextProvider<PDNContext>();

        [HttpGet]
        public string Metadata()
        {
            return _contextProvider.Metadata();
        }

        [HttpPost]
        public SaveResult SaveChanges(JObject saveBundle)
        {
            return _contextProvider.SaveChanges(saveBundle);
        }

        [HttpGet]
        public IQueryable<Category> Categories()
        {
            return _contextProvider.Context.Set<Category>().Include("SubCategories");
        }
        [HttpGet]
        public IQueryable<CompetencyAnswer> CompetencyAnswers()
        {
            return _contextProvider.Context.Set<CompetencyAnswer>().Include("CategoryAnswers.Category").Include("QuestionAnswers.Question");
        }

        //public IQueryable<dynamic> Bank()
        //{
        //    return from m in _contextProvider.Context.Set<Category>()
        //           select new {

        //           }
        //}
        [HttpGet]
        public IQueryable<CategoryQuestion> CategoryQuestions()
        {
            return _contextProvider.Context.Set<CategoryQuestion>().Include(m => m.Question);
        }
        [HttpGet]
        public IQueryable<CompetencyQuestion> QuestionList([FromUri] int[] categories)
        {
            var predicate = PredicateBuilder.False<CategoryQuestion>();
            foreach (int c in categories)
            {
                predicate = predicate.Or(p => p.CategoryId == c);
            }
            return _contextProvider.Context.Set<CategoryQuestion>().AsExpandable().Where(predicate).Select(m => m.Question).Distinct();
        }

        [HttpGet]
        public IQueryable<CompetencyAnswer> AnswerList([FromUri] int[] categories)
        {
            var predicate = PredicateBuilder.False<CategoryAnswer>();
            foreach (int c in categories)
            {
                predicate = predicate.Or(p => p.CategoryId == c);
            }
            return _contextProvider.Context.Set<CategoryAnswer>().AsExpandable().Where(predicate).Select(m => m.Answer).Distinct();
        }

        [HttpGet]
        public IQueryable<CompetencyQuestion> CompetencyQuestions()
        {
            return _contextProvider.Context.Set<CompetencyQuestion>().Include("CategoryQuestions.Category").Include("QuestionAnswers.Answer");
        }
        [HttpGet]
        public IQueryable<QuestionAnswer> QuestionAnswers()
        {
            return _contextProvider.Context.Set<QuestionAnswer>().Include("Question").Include("Answer");
        }
    }
}
